package com.wx.rabbitmq.message;


import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.wx.rabbitmq.MqConnection;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * @ClassName Producers
 * @Author wx
 * @Description 生产者-消息体定义
 * @Date 2018-11-22-22:31
 */
public class Producers {

    public static void main(String[] agrs) throws IOException, TimeoutException {
        //1.获取connection
        Connection connection = MqConnection.getConnection();

        //2.通过connection创建一个Channel
        Channel channel = connection.createChannel();


        Map<String, Object> headers = new HashMap<>();
        headers.put("my1", "111");
        headers.put("my2", "222");

        //3.消息装饰器
        AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
                //deliveryMode 1代表消息不持久 2代表消息持久化
                .deliveryMode(2)
                //消息编码
                .contentEncoding("UTF-8")
                //消息过期时间 5秒
                .expiration("5000")
                //自定义消息内容
                .headers(headers)
                .build();

        //3 通过Channel发送数据
        for (int i = 0; i < 5; i++) {
            String msg = "Hello RabbitMQ!";
            //1 exchange=交换机、 2 routingKey=队列对应的key、3 props = 消息装饰器、 4=消息体
            channel.basicPublish("", "test001", properties, msg.getBytes());
        }
        //4 记得要关闭相关的连接
        channel.close();
        connection.close();
    }

}
